var GLOBAL = {}; 
function W$(i){return document.getElementById(i)};
GLOBAL.namespace = function(str){ 
	var arr = str.split("."),o = GLOBAL; 
	for (i=(arr[0] == "GLOBAL") ? 1 : 0; i<arr.length; i++) { 
		o[arr[i]]=o[arr[i]] || {}; 
		o=o[arr[i]]; 
	} 
}
//class
var Class = {
  create: function() {
    return function() {
	  this.initialize.apply(this, arguments);
    }
  }
}

var Extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}
//event
GLOBAL.namespace("Event"); 
GLOBAL.Event.on = function(node,eventType,handler){ 
	node = typeof node == "string" ? document.getElementById(node) : node; 
	if(document.all){ 
		node.attachEvent("on"+eventType,callback); 
	} 
	else { 
		node.addEventListener(eventType,callback,false); 
	}
	function callback(e){
		handler.call(node,e);
	}
}

// getClassName
GLOBAL.namespace("Dom"); 
GLOBAL.Dom.getClassName = function(str,root,tag){ 
	if(root){ 
		root = typeof root == "string" ? 
		document.getElementById(root) : root; 
	} 
	else { 
		root = document.body;
	} 
	tag = tag || "*"; 
	var els = root.getElementsByTagName(tag),arr = [];  
	for(var i=0,n=els.length;i<n;i++){ 
		for(var j=0,k=els[i].className.split(" "),l=k.length;j<l;j++){ 
			if(k[j] == str){ 
				arr.push(els[i]); 
				break; 
			} 
		} 
	}
	return arr; 
}
// removeClass addClass
GLOBAL.Dom.addClass = function(node,str){ 
	if(!new RegExp("(^|\\s+)"+str).test(node.className)){ 
		node.className = node.className + " " + str; 
	}
} 
GLOBAL.Dom.removeClass = function(node,str){ 
	node.className = node.className.replace(new RegExp("(^|\\s+)"+str),""); 
}

// Tab funciton-------------------------------------------------------------------------
var Tab = Class.create();
Tab.prototype = {
	initialize:function(options){
		this.SetOptions(options);
		
		this.root = this.options.root; 
		this.currentClass = this.options.currentClass; 
		this.tabMenus1 = this.options.tabMenus; 
		this.tabContents1 = this.options.tabContents; 
		this.trigger = this.options.trigger || "click";
		this.handler = this.options.handler; 
		this.autoPlay = this.options.autoPlay; 
		this.showOne = this.options.showOne; 
		this.count = this.options.count; 
		this.cb = this.options.cb; 
		this.playTime = this.options.playTime || 3000;
		this.currentIndex = 0; 
		this.tabMenus = GLOBAL.Dom.getClassName(this.tabMenus1,this.root);
		this.tabContents = GLOBAL.Dom.getClassName(this.tabContents1,this.root);
		
		if(this.autoPlay){ 
			setInterval("this.autoHandler()",playTime); 
		}
		if(this.showOne){
			this.showItem(this.showOne);
		}
		
		var that = this;
		
		var funC = function(){
		var _index = this._index;
			(function(){
				//console.log(_index);
				that.showItem(_index);
				that.currentIndex = _index;
				if(typeof cb == 'function'){
					var cbFunc = cb(_index)
					return cbFunc;
				}
			})();
		}
		
		for(var i=0;i<this.tabMenus.length;i++){ 
			this.tabMenus[i]._index = i;
			GLOBAL.Event.on(this.tabMenus[i],this.trigger,funC);
		}
	},
	//设置默认属性
	SetOptions: function(options) {
		this.options = {//默认值
			root:			0,
			currentClass:	0,
			tabMenus1:		0,
			tabContents1:	0,
			trigger:        false,
			handler:		false,
			autoPlay:		null,
			count:			0,
			cb:				function(){},
			playTime:		0,
			currentIndex:	0,
			tabMenus:		0,
			tabContents:	0
		};
		Extend(this.options, options || {});
	},
	autoHandler:function(){ 
		currentIndex++; 
		if(currentIndex >= tabMenus.length){ 
			currentIndex = 0; 
		}
		this.showItem(currentIndex);
	},
	showItem:function(n){
		for(var i=0;i<this.tabContents.length;i++){ 
			this.tabContents[i].style.display = "none"; 
		}
		this.tabContents[n].style.display = "block"; 
		if(this.currentClass){ 
			var currentMenu = GLOBAL.Dom.getClassName(this.currentClass,this.root)[0];
			if(currentMenu){
				GLOBAL.Dom.removeClass(currentMenu,this.currentClass);
			} 
			GLOBAL.Dom.addClass(this.tabMenus[n],this.currentClass);
		} 
		if(this.handler){ 
			this.handler(n); 
		} 
	},
	test1:function(){
		console.log(Tab.prototype.tabContents);
	}
}


